package org.client.ui;
/**  * Date:2012/06/15  * 功能：QQ登入界面  */
import javax.swing.*;  
import org.client.tools.ClientTool;
import org.client.view.manage.Client2ServerThread;
import org.client.view.manage.IMLoginClientConServer;
import org.pojo.User;
import java.awt.*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class IMClientLogin extends JFrame implements MouseListener {     

JLabel jLabel0,jLabel1,jLabel2,jLabel3;
JPanel p;
JButton jButton1,jButton2;
JTextField jTextField;
JPasswordField jPasswordField;


public IMClientLogin() {
	JPanel p = new JPanel();
	p.setLayout(null);
	
	/*初始化背景*/
	ImageIcon backgrd = new ImageIcon("images/background.png");
	jLabel0 = new JLabel(backgrd);
	jLabel0.setBounds(0, 0, backgrd.getIconWidth(),backgrd.getIconHeight());
	this.getLayeredPane().add(jLabel0,new Integer(Integer.MIN_VALUE));
	JPanel jp = (JPanel) this.getContentPane();
	jp.setOpaque(false);
	p.setOpaque(false);
	
	/*处理组件*/
	
//中部.左	
	jLabel1 = new JLabel(new ImageIcon("images/im.png"));
	jLabel1.setBounds(10, 92, 80, 80);
	
//中部.右
	jTextField = new JTextField(20);
	jPasswordField = new JPasswordField(20);
	jLabel2 = new JLabel("注册账号", JLabel.RIGHT);
	jLabel3 = new JLabel("修改密码", JLabel.RIGHT);
	jTextField.setBounds(95, 100, 180, 30);
	jPasswordField.setBounds(95, 140, 180, 30);
	jLabel2.setBounds(280, 100, 60, 30);
	jLabel3.setBounds(280, 140, 60, 30);
	
//底部
	jButton1 = new JButton("关于");
	jButton2 = new JButton("登录");
	jButton1.setBounds(5, 215, 80, 20);
	jButton2.setBounds(260, 215, 80, 20);
 
//事件监听
	jLabel2.addMouseListener(this);
	jLabel3.addMouseListener(this);
	jButton1.addMouseListener(this);
	jButton2.addMouseListener(this);

//添加组件
	this.add(p);
	p.add(jLabel1);
	p.add(jTextField);
	p.add(jPasswordField);
	p.add(jLabel2);
	p.add(jLabel3);
	p.add(jButton1);
	p.add(jButton2);
	this.setTitle("2012Beta");
	this.setIconImage(Toolkit.getDefaultToolkit().createImage("images/im.png"));      
	this.setSize(355, 270);         
	this.setLocation(500,200);      
	this.setResizable(false);        
	this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);    
	this.setVisible(true);  
	}  
	
	public static void main(String[] args) {         
		// TODO Auto-generated method stub    
		IMClientLogin login = new IMClientLogin();  
		
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		Object source = e.getSource();
		if(source == jButton1){
			JOptionPane.showMessageDialog(this, "IM小组成员：杜聃, 周亮, 胡中华, 朱宇, 于佳民, 樊思明", "关于", JOptionPane.INFORMATION_MESSAGE);
		}
		else if(source == jButton2){
			//登录
			User u = new User(jTextField.getText(),jPasswordField.getText());
			boolean b = new IMLoginClientConServer().sendLoginInfoToServer(u);
			if(b){
				try {
					//创建主窗口
					IMMainWnd mainWnd=new IMMainWnd();
					ClientTool.mainWnd = mainWnd;
					//启动该通讯线程
					IMLoginClientConServer.ccst.start();
				} catch (Exception ex) {
					ex.printStackTrace();
					// TODO: handle exception
				}
				this.dispose();			
			}else
				JOptionPane.showMessageDialog(jButton2, "登录失败！", "失败", JOptionPane.INFORMATION_MESSAGE);
		}
		else if(source == jLabel2){
			//System.out.println("注册信息");
			//注册界面
			new IMRegistWnd();
		}
		else if(source == jLabel3){
			//System.out.println("修改密码");
			new IMAlterPw();
		}
	}
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		Object source = e.getSource();
		if (source == jLabel2) {
			jLabel2.setForeground(Color.BLUE);
		}
		else if (source == jLabel3) {
			jLabel3.setForeground(Color.BLUE);
		}
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		Object source = e.getSource();
		if (source == jLabel2) {
			jLabel2.setForeground(Color.BLACK);
		}
		else if (source == jLabel3) {
			jLabel3.setForeground(Color.BLACK);
		}
	}
} 